package com.it.trade.listener;

import com.hmall.common.domain.po.Order;
import com.it.trade.service.IOrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class ListeningPayStatus {

    private final IOrderService orderService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "trade.pay.success.queue"),
            exchange = @Exchange(value = "pay.direct",type = ExchangeTypes.DIRECT),
            key = "pay.success"
    ))
    public void listeningPaySuccess(Long orderId) {
        // 查询订单
        Order order = orderService.getById(orderId);
        // 判断订单是否是未支付
        if (order == null || !(order.getStatus() == 1)) {
            return;
        }
        orderService.markOrderPaySuccess(orderId);
    }
}
